package prc;

import java.util.ArrayList;
import java.util.Date;

public class Sectiune {
	private String nume;
	private Date lastPostDate;
	private User lastPostUser;
	public ArrayList<Topic> topicuri = new ArrayList<Topic>();
	
	public Sectiune(String nume){
		this.nume=nume;
		lastPostDate=null;
		lastPostUser=null;
	}
	
	public int getPostNr(){
		int nr=0;
		int i;
		
		for(i=0;i<topicuri.size();i++)
			nr+=topicuri.get(i).getNrPostari();
		
		return nr;
	}
	
	public int getTopicNr(){
		return topicuri.size();
	}
	
	public String getNume() {
		return nume;
	}
	public void setNume(String nume) {
		this.nume = nume;
	}

	public Date getLastPostDate() {
		return lastPostDate;
	}

	public void setLastPostDate(Date lastPostDate) {
		this.lastPostDate = lastPostDate;
	}

	/*public User getLastPostUser() {
		return lastPostUser;
	}*/
	
	public User getLastPostUser() {
		if(this.topicuri.size()>0){
			Topic topic = this.topicuri.get(0);
			if(topic != null){
				String postername = topic.getLastPoster();
				if(postername != null && postername.length()>0){
					return UserManagement.getUserByUsername(postername);
				}
			}
		}
		return null;
	}

	public void setLastPostUser(User lastPostUser) {
		this.lastPostUser = lastPostUser;
	}
	public void setFirst(Topic topic){
		int poz=-1;
		boolean found = false;
		for(poz = 0; poz<this.topicuri.size(); poz++){
			if(this.topicuri.get(poz) == topic){
				found = true;
				break;
			}
		}
		if(found){
			this.topicuri.remove(topic);
			this.topicuri.add(0, topic);
		}
	}
	public Topic findTopic(int id){
		for(int i=0; i<this.topicuri.size(); i++){
			if(this.topicuri.get(i).getID() == id){
				return this.topicuri.get(i);
			}
		}
		return null;
	}
	
}
